package com.milk.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.milk.system.common.result.PageResult;
import com.milk.system.common.result.Result;
import com.milk.system.model.form.OrdersForm;
import com.milk.system.model.query.OrdersPageQuery;
import com.milk.system.model.vo.OrdersPageVO;
import com.milk.system.service.OrdersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 订单信息表 前端控制器
 *
 * @author milk
 * @since 2024-05-04
 */
@Tag(name = "订单信息表接口")
@RestController
@RequestMapping("/api/order")
@RequiredArgsConstructor
public class OrdersController {

    private final OrdersService ordersService;

    @Operation(summary = "订单信息表分页列表")
    @GetMapping("/page")
    public PageResult<OrdersPageVO> listPagedOrderss(OrdersPageQuery queryParams) {
        IPage<OrdersPageVO> result = ordersService.listPagedOrderss(queryParams);
        return PageResult.success(result);
    }


    @Operation(summary = "新增订单信息表")
    @PostMapping
    public Result saveProducts(@RequestBody @Valid OrdersForm formData) {
        boolean result = ordersService.saveProducts(formData);
        return Result.judge(result);
    }


    @Operation(summary = "修改商品信息表")
    @PutMapping(value = "/{id}")
    public Result updateProducts(@Parameter(description = "商品信息表ID") @PathVariable Long id,
                                 @RequestBody @Validated OrdersForm formData) {
        boolean result = ordersService.updateProducts(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除商品信息表")
    @DeleteMapping("/{ids}")
    public Result deleteProductss(
            @Parameter(description = "商品信息表ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = ordersService.deleteProductss(ids);
        return Result.judge(result);
    }

    @Operation(summary = "商家信息表表单数据")
    @GetMapping("/{id}/form")
    public Result<OrdersForm> getMerchantsForm(
            @Parameter(description = "商家信息表ID") @PathVariable Long id
    ) {
        OrdersForm formData = ordersService.getMerchantsFormData(id);
        return Result.success(formData);
    }

}
